package com.milotnt.controller;

import com.milotnt.pojo.*;
import com.milotnt.service.ClassOrderService;
import com.milotnt.service.ClassTableService;
import com.milotnt.service.CoachService;
import com.milotnt.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhangMing [1157038410@qq.com]
 * @date 2021/8/21
 */

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private ClassTableService classTableService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ClassOrderService classOrderService;

    @Autowired
    private CoachService coachService;


    //跳转个人信息页面
    @RequestMapping("/toUserInfo")
    public String toUserInformation(Model model, HttpSession session) {
        Member member = (Member) session.getAttribute("user");
        model.addAttribute("member", member);
        return "userInformation";
    }

    //跳转修改个人信息页面
    @RequestMapping("/toUpdateInfo")
    public String toUpdateUserInformation(HttpSession session, Model model) {
        Member member = (Member) session.getAttribute("user");
        model.addAttribute("member", member);
        return "updateUserInformation";
    }

    //修改个人信息
    @RequestMapping("/updateInfo")
    public String updateUserInformation(HttpSession session, Member member) {
        Member member1 = (Member) session.getAttribute("user");

        member.setMemberAccount(member1.getMemberAccount());
        member.setCardClass(member1.getCardClass());
        member.setCardTime(member1.getCardTime());

        memberService.updateMemberByMemberAccount(member);
        session.setAttribute("user", member);
        return "userInformation";
    }

    //跳转我的课程页面
    @RequestMapping("/toUserClass")
    public String toUserClass(Model model, HttpSession session) {
        Member member = (Member) session.getAttribute("user");
        model.addAttribute("member", member);
        Integer memberAccount = member.getMemberAccount();
        List<ClassOrderDto> ClassDtoList = classOrderService.selectClassOrderByMemberAccount(memberAccount);
        model.addAttribute("ClassDtoList", ClassDtoList);
        return "userClass";
    }

    //退课
    public Member delete(Integer classOrderId, Integer memberAccount){
        ClassOrder order = classOrderService.getById(classOrderId);
        ClassTable table = classTableService.getById(order.getClassId());
        String s = table.getClassTime();
        int time = Integer.parseInt(s.substring(0, s.indexOf("分"))) * -1;
        Member member = memberService.updateCardClass(memberAccount, time);
        classOrderService.deleteByClassOrderId(classOrderId);
        return member;
    }
    @RequestMapping("delUserClass")
    public String deleteUserClass(Integer classOrderId, HttpSession session) {
        Member member = (Member) session.getAttribute("user");
        session.setAttribute("user", delete(classOrderId, member.getMemberAccount()));
        return "redirect:toUserClass";
    }

    @RequestMapping("delRecentClass")
    public String delRecentClass(Integer classOrderId, HttpSession session){
        Member member = (Member) session.getAttribute("user");
        session.setAttribute("user", delete(classOrderId, member.getMemberAccount()));
        return "redirect:toUserMain";
    }

    //跳转报名选课页面
    @RequestMapping("/toApplyClass")
    public String toUserApplyClass(Model model, HttpSession session) {
        Member member = (Member) session.getAttribute("user");
        List<ClassTable> classList = classTableService.findNotStart();
        List<ClassTableDto> classTableDtoList = new ArrayList<>();
        for (ClassTable classTable : classList) {
            ClassTableDto dto = new ClassTableDto();
            BeanUtils.copyProperties(classTable, dto);
            Coach coach = coachService.selectByCoachId(dto.getCoachId());
            dto.setCoach(coach.getCoachName());
            classTableDtoList.add(dto);
        }
        model.addAttribute("member", member);
        model.addAttribute("classTableDtoList", classTableDtoList);
        Object msg = session.getAttribute("msg");
        if(msg != null){
            model.addAttribute("msg", msg);
            session.removeAttribute("msg");
        }
        return "userApplyClass";
    }

    //报名选课
    @RequestMapping("/applyClass")
    public String userApplyClass(Integer classId, Model model, HttpSession session) {
        ClassTable classTable = classTableService.selectByClassId(classId);
        Member member = (Member) session.getAttribute("user");

        String s = classTable.getClassTime();
        int time = Integer.parseInt(s.substring(0, s.indexOf("分")));
        // 判断剩余时长是否足够
        if(member.getCardClass() < time){
            session.setAttribute("msg", "剩余课时时长不足");
            return "redirect:toApplyClass";
        }

        Integer classId1 = classTable.getClassId();
        String className = classTable.getClassName();
        Integer coachId = classTable.getCoachId();
        String coach = coachService.selectByCoachId(coachId).getCoachName();
        String classBegin = classTable.getClassBegin();
        String memberName = member.getMemberName();
        Integer memberAccount = member.getMemberAccount();

        // 创建classOrder并赋值
        ClassOrder classOrder = new ClassOrder(classId1, className, coach, memberName, memberAccount, classBegin);
        Integer memberAccount1 = member.getMemberAccount();
        ClassOrder classOrder1 = classOrderService.selectMemberByClassIdAndMemberAccount(classId1, memberAccount1);

        if (classOrder1 == null) {
            classOrderService.insertClassOrder(classOrder);
            Member member1 = memberService.updateCardClass(member.getMemberAccount(), time);
            session.setAttribute("user", member1);
            return "redirect:toUserClass";
        }else {
            session.setAttribute("msg", "当前课程已报名");
            return "redirect:toApplyClass";
        }
    }
}
